using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.ComponentModel;
using Portal.API;

namespace Portal
{
	/// <summary>
	/// Collection of Helper Methods. For internal use only.
	/// </summary>
	public sealed class Helper
	{
		/// <summary>
		/// Helper-Class must not be instantiated, so set the constructor to private.
		/// </summary>
		private Helper() { }

		/// <summary>
		/// Returns the path to the Portal CSS-File.
		/// </summary>
		public static string CssPath
		{
			get
			{
				string filePath;
				HttpContext context = HttpContext.Current;
				if (context != null)
				{
					filePath = context.Request.ApplicationPath;
					if (!filePath.EndsWith("/"))
						filePath += "/";
				}
				else
					filePath = "";

				return filePath + System.Configuration.ConfigurationManager.AppSettings["CssFile"];
			}
		}

		/// <summary>
		/// Retruns the path to the Portal CSS-File for the HTML-Edit Mode.
		/// </summary>
		public static string CssAdminPath
		{
			get
			{
				string subPath = System.Configuration.ConfigurationManager.AppSettings["CssFileForEdit"];
				if (string.IsNullOrEmpty(subPath))
				{
					subPath = "Data/Resources/Admin.css";
				}
				string filePath;
				HttpContext context = HttpContext.Current;
				if (context != null)
				{
					filePath = context.Request.ApplicationPath;
					if (!filePath.EndsWith("/"))
						filePath += "/";
				}
				else
					filePath = "";

				return filePath + subPath;
			}
		}

		/// <summary>
		/// Returns the proper edit ascx Control. Uses the current Page to load the Control.
		/// If the user has no right a error control is returned
		/// </summary>
		/// <param name="p">The current Page</param>
		/// <returns>Edit ascx Control</returns>
		public static Control GetEditControl(Page p)
		{
			PortalDefinition.Tab tab = PortalDefinition.CurrentTab;
			PortalDefinition.Module m = tab.GetModule(p.Request["moduleRef"]);

			if (!UserManagement.HasEditRights(HttpContext.Current.User, m.roles))
			{
				return GetNoAccessControl();
			}
			m.LoadModuleSettings();
			Module em = null;
			if (m.moduleSettings != null)
			{
				// Module Settings are present, use custom ascx Control
				em = (Module)p.LoadControl(AppConfig.GetModuleVirtualPath(m.type) + m.moduleSettings.editCtrl);
			}
			else
			{
				// Use default ascx control (Edit[type].ascx)
				em = (Module)p.LoadControl(AppConfig.GetModuleVirtualPath(m.type) + "Edit" + m.type + ".ascx");
			}

			// Initialize the control
			em.InitModule(
					tab.reference,
					m.reference,
					m.type,
					AppConfig.GetModuleDataVirtualPath(m.type),
					true);

			return em;
		}

		public static Control GetNoAccessControl()
		{
			// No rights, return a error Control
			System.Web.UI.WebControls.Label l = new System.Web.UI.WebControls.Label();
			l.CssClass = "Error";
			l.Text = "Access denied!";
			return l;
		}

		/// <summary>
		/// Returns the requested Tab.
		/// If the user has no right null is returned.
		/// </summary>
		/// <param name="tabRef"></param>
		/// <returns></returns>
		public static PortalDefinition.Tab GetEditTab(string tabRef)
		{
			PortalDefinition.Tab editTab = null;

			if (HttpContext.Current.User.IsInRole(AppConfig.AdminRole))
			{
				PortalDefinition pd = PortalDefinition.Load(Helper.GetPortalConfig(tabRef));
				editTab = pd.GetTab(tabRef);
			}
			return editTab;
		}

		public static string GetEditLink(PortalDefinition.Module module)
		{
			bool isInplace = module.moduleSettings != null && module.moduleSettings.IsInplaceEdit;
			if (isInplace)
			{
				return AppConfig.GetTabUrl(HttpContext.Current.Request["tabRef"]) + "?Edit=Content&moduleRef=" + module.reference + "&tabRef=";
			}
			else
			{
				return "EditPageTable.aspx?moduleRef=" + module.reference + "&tabRef=" + HttpContext.Current.Request["tabRef"];
			}
		}

		public static string GetCancelEditLink()
		{
			return GetTabLink(HttpContext.Current.Request["TabRef"]);
		}

		public static string GetEditModuleLink(string moduleRef)
		{
			return "EditModule.aspx?moduleRef=" + moduleRef + "&tabRef=" + HttpContext.Current.Request["tabRef"];
		}

		public static string GetEditTabLink()
		{
			return GetEditTabLink(HttpContext.Current.Request["tabRef"]);
		}

		public static string GetEditTabLink(string tabRef)
		{
			return "AdminDefault.aspx?pageView=AdminTab&tabRef=" + tabRef;
		}

		public static string GetTabLink(string reference)
		{
			return AppConfig.GetTabUrl(reference);
		}

		public static string GetPortalConfig(string tabRef)
		{
			return (tabRef.IndexOf(AppConfig.PORTAL_ADMIN.ToLower()) != -1 ? AppConfig.PORTAL_ADMIN : "");
		}

		/// <summary>
		/// Checks if this module is requested to appear in edit mode.
		/// </summary>
		public static bool IsEditModuleRequested(PortalDefinition.Module module)
		{
			HttpRequest request = HttpContext.Current.Request;
			string editMode = request["Edit"];
			string moduleRef = request["ModuleRef"];
			bool dataAvailable = !string.IsNullOrEmpty(editMode) && !string.IsNullOrEmpty(moduleRef);
			return dataAvailable && editMode == "Content" && moduleRef == module.reference
				&& module.moduleSettings.HasEditCtrl;
		}
	}
}
